package com.culturalCenter.dataCenter.service.impl.site;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.culturalCenter.dataCenter.Utils.DateFormatUtil;
import com.culturalCenter.dataCenter.Utils.MapperUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.site.SiteUsedResponse;
import com.culturalCenter.dataCenter.customEntity.responseParam.site.UsedRateResponse;
import com.culturalCenter.dataCenter.customEntity.site.CustomSiteBooking;
import com.culturalCenter.dataCenter.customEntity.site.CustomSiteUsedCount;
import com.culturalCenter.dataCenter.customEntity.site.CustomSiteUsedHistory;
import com.culturalCenter.dataCenter.entity.site.SiteUsedCount;
import com.culturalCenter.dataCenter.mapper.site.SiteBookingMapper;
import com.culturalCenter.dataCenter.mapper.site.SiteMapper;
import com.culturalCenter.dataCenter.mapper.site.SiteUsedCountMapper;
import com.culturalCenter.dataCenter.service.site.SiteUsedCountService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/****
 * @Author:
 * @Description:SiteUsedCount业务层接口实现类
 * @Date
 *****/
@Service
public class SiteUsedCountServiceImpl implements SiteUsedCountService {

    @Resource
    private SiteUsedCountMapper siteUsedCountMapper;
    @Resource
    private SiteMapper siteMapper;
    @Resource
    private MapperUtil mapperUtil;
    @Resource
    private SiteBookingMapper siteBookingMapper;

    @Override
    public PageInfo<SiteUsedCount> findPage(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> siteUsedCountMapper.findPageAll());
    }

    @Override
    public Integer delete(String id) {
        return siteUsedCountMapper.deleteById(id);
    }

    @Override
    public Integer update(SiteUsedCount siteUsedCount) {
        return siteUsedCountMapper.update(siteUsedCount);
    }

    @Override
    public Integer add(SiteUsedCount siteUsedCount) {
        return siteUsedCountMapper.insert(siteUsedCount);
    }

    @Override
    public SiteUsedCount findById(String id) {
        return siteUsedCountMapper.queryById(id);
    }

    @Override
    public List<SiteUsedCount> findAll(SiteUsedCount siteUsedCount) {
        return (List<SiteUsedCount>) siteUsedCountMapper.queryAll(siteUsedCount);
    }

    @Override
    public UsedRateResponse getUsageRate(Date date, Date year, String near) {
        UsedRateResponse usedRateResponse = new UsedRateResponse();
        String year1 = null;
        String month = null;
        //年
        if (date == null && near == null) {
            year1 = DateFormatUtil.getYear(year);
            month = "0";
        }
        //月
        if (year == null && near == null) {
            month = DateFormatUtil.getMonth(date);
            year1 = DateFormatUtil.getYear(date);
        }
        //近7天
        if (date == null && year == null && near.equals("-1")) {
            year1 = DateFormatUtil.getYear(new Date());
            month = "-1";
        }

        //列表
        Map<String, List<CustomSiteUsedCount>> siteMap = new HashMap<>();
        List<String> siteArea = siteMapper.getSiteArea();
        if (CollectionUtils.isNotEmpty(siteArea)) {
            for (String area : siteArea) {
                siteMap.put(area, siteUsedCountMapper.queryByDate(area, year1, month));
            }
        }
        usedRateResponse.setSiteMap(siteMap);
        //热门场地
        List<CustomSiteUsedCount> hotSiteList = siteUsedCountMapper.queryHotSiteListByDate(year1, month);
        if (CollectionUtils.isNotEmpty(hotSiteList)) {
            usedRateResponse.setHotSiteList(hotSiteList);
        }
        //闲置场地
        List<CustomSiteUsedCount> unusedSiteList = siteUsedCountMapper.queryUnusedSiteByDate(year1, month);
        if (CollectionUtils.isNotEmpty(unusedSiteList)) {
            usedRateResponse.setUnusedSiteList(unusedSiteList);
        }
        return usedRateResponse;
    }

    @Override
    public SiteUsedCount findOnResult(SiteUsedCount siteUsedCount) {
        return (SiteUsedCount) mapperUtil.getOneResultByClass(siteUsedCountMapper, siteUsedCount);
    }

    @Override
    public SiteUsedResponse getSiteUsed(String siteId, Date date) {

        SiteUsedCount siteUsedCount = new SiteUsedCount();
        siteUsedCount.setSiteId(siteId);

        date = (date == null) ? new Date() : date;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String date1 = simpleDateFormat.format(date);
        //将获得的年月分割成年和月
        String[] dateArray = date1.split("-");
        siteUsedCount.setYear(Integer.valueOf(dateArray[0]));
        siteUsedCount.setMonth(Integer.valueOf(dateArray[1]));
        //月统计信息
        siteUsedCount = (SiteUsedCount) mapperUtil.getOneResultByClass(siteUsedCountMapper, siteUsedCount);
        SiteUsedResponse siteUsedResponse = new SiteUsedResponse();
        if (siteUsedCount != null) {
            Integer usedNum = siteUsedCount.getUsedNum();
            if (usedNum != null) {
                siteUsedResponse.setUsedNum(usedNum);
            }

            Integer alertsNum = siteUsedCount.getAlertsNum();
            if (alertsNum != null) {
                siteUsedResponse.setAlertsNum(alertsNum);
            }
        }
        //月Map
        List<CustomSiteBooking> bookingList = siteBookingMapper.selectSiteBookingByDate(siteId, date);

        Map<String, List<CustomSiteBooking>> siteBookingMap = new LinkedHashMap<>();
        List<CustomSiteBooking> bookingDayList;
        if (CollectionUtils.isNotEmpty(bookingList)) {
            //用for循环得到单次预约数据
            for (CustomSiteBooking customSiteBooking : bookingList) {
                //获取并格式化beginTime
                String key = DateFormatUtil.format(customSiteBooking.getBeginTime());
                //如果siteBookingMap不包含该beginTime
                if (!siteBookingMap.containsKey(key)) {
                    bookingDayList = new ArrayList<>();
                    bookingDayList.add(customSiteBooking);
                    //放入该beginTime和对应的排期
                    siteBookingMap.put(key, bookingDayList);
                } else {
                    //如果siteBookingMap有该beginTime，则直接存入对应的customSiteBooking
                    siteBookingMap.get(key).add(customSiteBooking);
                }
            }
        }
        siteUsedResponse.setSiteBookingMap(siteBookingMap);
        return siteUsedResponse;
    }

    @Override
    public PageInfo<CustomSiteUsedHistory> getUsageRateHistory(int pageNum, int pageSize, String siteName, String startDate, String endDate) {
        //没传日期就显示本年的
        if (StrUtil.isBlank(startDate) & StrUtil.isBlank(endDate)) {
            startDate = String.valueOf(DateUtil.beginOfYear(new Date()));
            endDate = DateUtil.today();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<CustomSiteUsedHistory> siteList = siteUsedCountMapper.getUsageRateHistory(siteName);
        String finalStartDate = startDate;
        String finalEndDate = endDate;
        siteList.forEach(site -> site.setUsedRate(siteUsedCountMapper.getUsedRate(site.getSiteId(), finalStartDate, finalEndDate)));
        return new PageInfo<>(siteList);
    }

}
